// CherryMerry ( Mikhail Tomilenko ), Reverse Entertainment, 2021-2024


#include "../Public/PlayerUIManager.h"
#include <NifestGame/Core/Public/NF_GameInstance.h>

#include <Kismet/GameplayStatics.h>

UPlayerUIManager::UPlayerUIManager()
{
	PrimaryComponentTick.bCanEverTick = false;

	/* MainMenu widget */
	MainMenuClass = nullptr;
	MainMenu = nullptr;

	/* HUD widget */
	PlayerHUDClass = nullptr;
	PlayerHUDMenu = nullptr;

	/* Respawn widget */
	RespawnMenuClass = nullptr;
	RespawnMenu = nullptr;
}


void UPlayerUIManager::BeginPlay()
{
	Super::BeginPlay();
}

void UPlayerUIManager::CreateMainMenu()
{
	APlayerController* PlayerController = UGameplayStatics::GetPlayerController(this, 0);

	if (MainMenuClass)
	{
		MainMenu = CreateWidget<UMainMenu>(PlayerController, MainMenuClass);

		if (MainMenu)
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Widget is valid")));

			MainMenu->AddToViewport();

			FInputModeUIOnly InputUIOnlyModeData;
			InputUIOnlyModeData.SetWidgetToFocus(MainMenu->TakeWidget());
			InputUIOnlyModeData.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
			PlayerController->SetInputMode(InputUIOnlyModeData);
			PlayerController->bShowMouseCursor = true;
		}
		else
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Widget is not valid")));
		}
	}
}

void UPlayerUIManager::CreateHUDWidget()
{
	APlayerController* PlayerController = UGameplayStatics::GetPlayerController(this, 0);

	if (PlayerHUDClass)
	{
		PlayerHUDMenu = CreateWidget<UPlayerHUD>(PlayerController, PlayerHUDClass);

		if (IsValid(PlayerHUDMenu))
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Widget is valid")));

			PlayerHUDMenu->AddToViewport();

			FInputModeGameOnly InputGameOnlyModeData;
			PlayerController->SetInputMode(InputGameOnlyModeData);
			PlayerController->bShowMouseCursor = false;
		}
		else
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Widget is not valid")));
		}
	}
}

void UPlayerUIManager::CreateRespawnWidget()
{
	APlayerController* PlayerController = UGameplayStatics::GetPlayerController(this, 0);

	if (RespawnMenuClass)
	{
		RespawnMenu = CreateWidget<URespawnMenu>(PlayerController, RespawnMenuClass);

		if (IsValid(RespawnMenu))
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Widget is valid")));

			RespawnMenu->AddToViewport();

			FInputModeUIOnly InputUIOnlyModeData;
			InputUIOnlyModeData.SetWidgetToFocus(RespawnMenu->TakeWidget());
			InputUIOnlyModeData.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
			PlayerController->SetInputMode(InputUIOnlyModeData);
			PlayerController->bShowMouseCursor = true;
		}
		else
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Widget is not valid")));
		}
	}
}

void UPlayerUIManager::RemoveRespawnWidget()
{
	APlayerController* PlayerController = UGameplayStatics::GetPlayerController(this, 0);

	if (RespawnMenuClass)
	{
		if (IsValid(RespawnMenu))
		{
			RespawnMenu->RemoveFromParent();
			FInputModeGameOnly InputGameOnlyModeData;
			PlayerController->SetInputMode(InputGameOnlyModeData);
			PlayerController->bShowMouseCursor = false;
		}
	}
}

